// pages/my/linprice.js
import Router from '../../utils/router.js'
import base from '../../config/base.js'
import util from '../../utils/util.js'
import conchService from '../../services/myindex.js'
import myindexService from '../../services/myindex.js';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    link: 'https://kpxd.zhengdianhaohuo.com/h5/#/buyer/win3_1119',
    loadflag: true,
    listflag: false,
    linpriceimg: base.static + '/my/nobeike.png',
    linq_bg: base.static + '/my/linq_bg.jpg',
    conchnum: '0',
    page: 1,
    per_page: 20,
    list: [],
    listlength: 1,
    arrow_img: base.static + 'detail/lin-r.png',
    
  },
  /**
   * 
   */
  getlinprice:function(){
    let that = this,
      loadflag = that.data.loadflag;
    if (!loadflag){
      return ;
    }
    let param = {},
      page = that.data.page,
      per_page = that.data.per_page;
    param.page = page;
    param.per_page = per_page;
    conchService.conchbill(param, res => {
      if (res && res.code == 200) {
        let list = res.data.dataList,
          dataCount = res.data.dataCount;
        for (let i in list) {
          if (list[i].bill_type == 1) {
            list[i].bill_type_str = '+';
            list[i].bill_type_col = 'coljia';
          }else if (list[i].bill_type == 2) {
            list[i].bill_type_str = '-';
            list[i].bill_type_col = 'coljian';
          }
          if (list[i].bill_money) { }
          if (list[i].bill_money != null && list[i].bill_money != '' && !isNaN(Number(list[i].bill_money))) {
             //list[i].bill_money = util.toDecimal2(util.floatAccMul(list[i].bill_money, 1));
          }
          if (list[i].bill_balance != null && list[i].bill_balance != '' && !isNaN(Number(list[i].bill_balance))) {
            list[i].bill_balance = util.toDecimal2(util.floatAccMul(list[i].bill_balance, 1));
          }
        }
        list = that.data.list.concat(list);
        if (dataCount <= list.length) {
          loadflag = false;
        }
        that.setData({
          list,
          loadflag,
        });
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
      }
    }, res => {
      that.setData({
        loadflag,
        listflag: true,
        listlength: that.data.list.length
      });
      if (res && res.statusCode != 200) {
        if (page > 1) {
          page--;
        } else {
          page = 1;
        }
        that.setData({
          page
        });
      }
    });
  },
  //去每日任务页面
  godailytask(){
    Router.go('dailytasks');
  },
  //获取买家贝壳数
  getconchnum: function () {
    let that = this;
    let param = {};
    myindexService.getconch(param, res => {
      if (res && res.code == 1) {
        let conchnum = res.data.conch;
        // if (conchnum !== null && conchnum !== undefined && conchnum !== '' && !isNaN(Number(conchnum))) {
        //   conchnum = util.toDecimal2(util.floatAccMul(conchnum, 1));
        // }
        that.setData({
          conchnum
        });
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
      }
    }, res => {
      wx.stopPullDownRefresh();
    });
  },
  //去帮助说明页面
  goConchRule(){
    let link = encodeURIComponent(this.data.link);
    if (link) {
      Router.go('web', `url=${link}`, 'navigateTo');
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getlinprice();
    this.getconchnum();
    // this.setData({
    //    conchnum: options.conchnum ? options.conchnum : '0'
    // });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let that = this,
      listflag = that.data.listflag,
      loadflag = that.data.loadflag;
    if (!listflag && loadflag) {
      return;
    }
    let page = that.data.page;
    page++;
    that.setData({
      page,
      listflag: false
    });
    that.getlinprice();
  }
})